package com.wzh.glsc.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.constant.WareConstant;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;
import com.wzh.glsc.ware.dao.PurchaseDetailDao;
import com.wzh.glsc.ware.entity.PurchaseDetailEntity;
import com.wzh.glsc.ware.service.PurchaseDetailService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key=(String)params.get("key");
        QueryWrapper<PurchaseDetailEntity> purchaseDetailEntityQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty((String)params.get("status"))) {
            purchaseDetailEntityQueryWrapper.eq("status", (String)params.get("status"));
        }
        if (StringUtils.isNotEmpty((String)params.get("wareId"))) {
            purchaseDetailEntityQueryWrapper.eq("ware_id", (String)params.get("wareId"));
        }
        if (StringUtils.isNotEmpty(key)) {
            purchaseDetailEntityQueryWrapper.and((obj)->{
                obj.eq("purchase_id", key)
                        .or().like("sku_num", key)
                        .or().like("sku_price", key)
                        .or().like("status", key);
            });
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                purchaseDetailEntityQueryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void updateStatusByPurchaseId(Long id) {
        this.update(new UpdateWrapper<PurchaseDetailEntity>()
                .eq("purchase_id", id)
                .set("status", WareConstant.PurchaseDetailsEnum.BE_PURCHASING.getCode()));
    }

}